<template>
  <div class="app-container">
    <el-menu :default-active="listQuery.tab" class="el-menu-demo" mode="horizontal" @select="handleSelect">
      <el-menu-item index="my">我的</el-menu-item>
      <el-menu-item index="group" v-if="departProfitSettlement">本组</el-menu-item>
      <el-menu-item index="all" v-if="allProfitSettlement">全部</el-menu-item>
    </el-menu>
    <!--搜索-->
    <el-form :inline="true" :model="listQuery" size="mini" label-width="100px" style="margin-top:40px">
      <el-form-item label="当前归属人：">
        <el-select v-model="listQuery.chargeId" filterable clearable>
          <el-option
            v-for="item in userOptionList"
            :key="item.userId"
            :label="item.userName"
            :value="item.userId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="实际采购人：">
        <el-select  v-model="listQuery.purchaseUserId" filterable clearable>
          <el-option
            v-for="item in userOptionList"
            :key="item.userId"
            :label="item.userName"
            :value="item.userId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="销售合同编号：">
        <el-input v-model="listQuery.saleOrderSn" class="input-width" style="width:195px"></el-input>
      </el-form-item>
      <el-form-item label="采购单号：">
        <el-input v-model="listQuery.purchaseOrderSn" class="input-width" style="width:195px"></el-input>
      </el-form-item>
      <el-form-item label="合同结束时间：">
        <el-date-picker
          v-model="listQuery.deliverDate"
          type="daterange"
          format="yyyy 年 MM 月 dd 日"
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button
          @click="handleSearchList()"
          type="primary">
          查询
        </el-button>
      </el-form-item>
    </el-form>
    <div style="font-size:14px;line-height: 30px;padding:10px 0 ">
      采销毛利汇总：<span style="font-weight: bold">{{ psAllProfit }}</span>元
    </div>
    <div class="table-container" style="margin-top:0">
      <el-table
        v-loading="listLoading"
        :data="tableData"
        border
        style="width: 100%;">
        <el-table-column  label="销售合同编号" min-width="150" align="center">
          <template slot-scope="scope">
              {{scope.row.saleOrderSn}}
          </template>
        </el-table-column>
        <el-table-column  label="采购单号" min-width="150"  align="center">
          <template slot-scope="scope">
            {{scope.row.purchaseOrderSn}}
          </template>
        </el-table-column>
        <el-table-column  label="合同结束时间" min-width="150"  align="center">
          <template slot-scope="scope">
            {{scope.row.deliverDate}}
          </template>
        </el-table-column>
        <el-table-column  label="归属人" min-width="100" align="center">
          <template slot-scope="scope">
            {{scope.row.ownershipUser}}
          </template>
        </el-table-column>
        <el-table-column  label="实际采购人" min-width="150" align="center">
          <template slot-scope="scope">
            {{scope.row.purchaseUser}}
          </template>
        </el-table-column>
        <el-table-column  label="销售毛利" min-width="150" align="center">
          <template slot-scope="scope">
            {{scope.row.saleProfit}}
          </template>
        </el-table-column>
        <el-table-column  label="归属毛利" min-width="150" align="center">
          <template slot-scope="scope">
            {{scope.row.ownershipProfit}}
          </template>
        </el-table-column>
        <el-table-column  label="账期资金占用" min-width="150" align="center">
          <template slot-scope="scope">
            {{scope.row.saleCost}}
          </template>
        </el-table-column>
        <el-table-column  label="采购资金占用" min-width="150" align="center">
          <template slot-scope="scope">
            {{scope.row.purchaseCost}}
          </template>
        </el-table-column>
        <el-table-column  label="客户违约" min-width="150" align="center">
          <template slot-scope="scope">
            {{scope.row.customerBreakCost}}
          </template>
        </el-table-column>
        <el-table-column  label="发货逾期" min-width="150" align="center">
          <template slot-scope="scope">
            {{scope.row.deliverOutTimeCost}}
          </template>
        </el-table-column>
<!--        <el-table-column  label="运费" min-width="150" align="center">-->
<!--          <template slot-scope="scope">-->
<!--            {{scope.row.saleOrderSn}}-->
<!--          </template>-->
<!--        </el-table-column>-->
<!--        <el-table-column  label="其他杂费" min-width="150" align="center">-->
<!--          <template slot-scope="scope">-->
<!--            {{scope.row.saleOrderSn}}-->
<!--          </template>-->
<!--        </el-table-column>-->
        <el-table-column  label="采销毛利" min-width="150" align="center">
          <template slot-scope="scope">
            {{scope.row.psProfit}}
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[10,15,20]"
        :total="count">
      </el-pagination>
    </div>
  </div>
</template>
<script>
import {getOrderUsersList} from '@/api/partner'
import {getProfitList,psProfitSummary} from '@/api/order'
import { mapGetters } from 'vuex'
import store from '@/store'
const defaultListQuery={
  "tab" : "my", //my:我的,group:组,all:全部
  "chargeId" : '',  //归属人ID
  "purchaseUserId" : '', //实际采购人ID
  "saleOrderSn" : "", //销售订单号
  "purchaseOrderSn" : "", //采购订单号
  "deliverDate" : [], //发货日期
  "pageNum"   : 1,
  "pageSize"  : 10


};
export default {
  name: "index",
  data(){
    return {
      listQuery: JSON.parse(JSON.stringify(defaultListQuery)),
      tableData: [],
      count: 0,
      listLoading: false,
      userOptionList:[],
      roles:store.getters.roles ? store.getters.roles : [],
      psAllProfit:''
    }
  },
  components: {

  },
  computed: {
    ...mapGetters([
      'name',
      'mobile',
      'userId'
    ]),
    departProfitSettlement(){
      if(this.roles.indexOf("departProfitSettlement") != -1){
        return true
      }else{
        return false
      }
    },
    allProfitSettlement(){
      if(this.roles.indexOf("allProfitSettlement") != -1){
        return true
      }else{
        return false
      }
    },

  },
  created() {
    this.getList()
    this.getSum()
    this.getUserOptionList()

  },
  filters: {


  },
  methods: {
    //获取员工下拉选项
    getUserOptionList(){
      getOrderUsersList().then((res)=>{
        this.userOptionList = res;
      })
    },

    /**
     * 切换选择状态
     */
    handleSelect(key, keyPath){
      this.listQuery = JSON.parse(JSON.stringify(defaultListQuery))
      this.listQuery.tab = key;
      this.handleSearchList();
    },
    handleSizeChange(val){
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val){
      this.listQuery.pageNum = val;
      this.getList();
    },

    handleSearchList(){
      this.listQuery.pageNum = 1;
      this.getList();
      this.getSum()
    },

    //获取列表
    getList() {
      this.listLoading = true;
      getProfitList(this.listQuery).then(response => {
        this.tableData = response.list;
        this.count = response.count;
        this.listLoading = false;
      }).catch(()=>{
        this.listLoading = false;
      });
    },

    getSum(){
      psProfitSummary(this.listQuery).then((res)=>{
          this.psAllProfit = res.psAllProfit
      })
    },


  },
}
</script>


